package cn.hls.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.hls.dao.CarAttachDao;
import cn.hls.entity.CarAttach;
import cn.hls.entity.vo.PageInfo;
import cn.hls.exception.BussinessException;
import cn.hlsconstant.DataAssessEnum;

 
/**
 *@fileName: CarAttachDaoImpl.java
 *@package: cn.christ.dao.impl
 *@Description:  车辆基本信息数据接口实现类
 *@author: yinghui.zhang
 *@email: uuhui@163.com
 *@date: 2015-7-18
 */
@Component("carAttachDao")
public class CarAttachDaoImpl extends BaseDaoImpl<CarAttach> implements CarAttachDao {

	@Override
	public PageInfo<CarAttach> loadCarAttachInfo(CarAttach entity, PageInfo<CarAttach> page) throws Exception {
		// 定义查询Hql语句
		StringBuilder selectHql=new StringBuilder("from CarAttach p where p.deleted=? ");
		//定义汇总的hql语句
		StringBuilder countHql=new StringBuilder();
		// 定义查询参数
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(false);
		if (entity != null) {
			//按车牌号
			 if(StringUtils.hasText(entity.getLicenseNum())){
				 selectHql.append("and licenseNum=? ");
				 paramList.add(entity.getLicenseNum().trim());
			 }
			 //按类型查询
			 if(entity.getType()!=null){
				 selectHql.append("and p.type=? ");
				 paramList.add(entity.getType());
			 }
			 //按附件名称查询
			 if(StringUtils.hasText(entity.getFileName())){
				 selectHql.append("and p.fileName like ? ");
				 paramList.add("%"+entity.getFileName().trim()+"%");
			 }
		}
		countHql.append("select count(p.id) ").append(selectHql);
		selectHql.append(" order by p.createTime");
		return find(selectHql.toString(), countHql.toString(), paramList.toArray(), page);
	}
	
	@Override
	public void delete(Object entityId) throws Exception {
		String []ids=(String[])entityId;
		StringBuilder hql=new StringBuilder("delete CarAttach  where id in (");
		List<Object>params=new ArrayList<Object>();
		for (String id:ids){
			hql.append("?,");
			params.add(Long.valueOf(id));
		}	
		hql.delete(hql.length()-1, hql.length());
		hql.append(")");
		 try {
			executeHQLUpdate(hql.toString(), params.toArray());
		} catch (Exception e) {
			e.printStackTrace();
			throw new BussinessException(DataAssessEnum.DELETE_ERROR,"删除车辆信息失败" );
		}
	}

	 
	 
	@Override
		public boolean exists(CarAttach entity)throws Exception{
			StringBuilder selectHql=new StringBuilder("select count(id) from CarAttach where deleted=? ");
			List<Object>params=new ArrayList<Object>();
			params.add(false);
			if(entity!=null){
				if(StringUtils.hasText(entity.getLicenseNum())){//姓名
					selectHql.append("and licenseNum=? ");
					params.add(entity.getLicenseNum().trim());
				}
				//按类型查询
				if(entity.getType()!=null){
					selectHql.append("and type=? ");
					params.add(entity.getType());
				}
				//按附件名称查询
				if(StringUtils.hasText(entity.getFileName())){
					selectHql.append("and fileName=? ");
					params.add(entity.getFileName().trim());
				}
			
			}
		    Long c=countByHql(selectHql.toString(), params.toArray());
			return c.intValue()>0?true:false;
	}

	@Override
	public List<CarAttach> loadCarAttachList(CarAttach entity) throws Exception {
		StringBuilder selectHql=new StringBuilder("from CarAttach p where p.deleted=? ");
		List<Object>paramList=new ArrayList<Object>();
		if (entity != null) {
			//按车牌号
			 if(StringUtils.hasText(entity.getLicenseNum())){
				 selectHql.append("and licenseNum like ? ");
				 paramList.add("%"+entity.getLicenseNum().trim()+"%");
			 }
			 //按类型查询
			 if(entity.getType()!=null){
				 selectHql.append("and p.type=? ");
				 paramList.add(entity.getType());
			 }
			 //按附件名称查询
			 if(StringUtils.hasText(entity.getFileName())){
				 selectHql.append("and p.fileName like ? ");
				 paramList.add("%"+entity.getFileName().trim()+"%");
			 }
		}
		return findByHql(selectHql.toString(), paramList.toArray());
	}
	
}
